package city.spring.modules.medicalstation.drugdeposit.service.impl;

import city.spring.modules.medicalstation.drugdeposit.entity.DrugdepositEntity;
import city.spring.modules.medicalstation.drugdeposit.repository.DrugdepositRepository;
import city.spring.modules.medicalstation.drugdeposit.service.DrugdepositService;
import city.spring.modules.medicalstation.drugdeposit.vo.DrugdepositVO;
import city.spring.modules.medicalstation.drugdepositDrugExt.entity.DrugdepositDrugExtEntity;
import city.spring.modules.medicalstation.drugdepositDrugExt.repository.DrugdepositDrugExtRepository;
import city.spring.modules.system.entity.UserEntity;
import city.spring.modules.system.service.UserService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

import static city.spring.utils.UserUtils.getPrincipal;

/**
 * Service：医护站药品缴存
 *
 * @author 谢金婷
 * @date 2020-10-13 11:44:37
 */
@CacheConfig(cacheNames = {DrugdepositServiceImpl.CACHE_NAME})
@Transactional(rollbackFor = Exception.class)
@Service
public class DrugdepositServiceImpl extends ServiceImpl<DrugdepositRepository, DrugdepositEntity> implements DrugdepositService {
    private final UserService userService;
    private final DrugdepositDrugExtRepository drugdepositDrugExtRepository;

    public DrugdepositServiceImpl(UserService userService, DrugdepositDrugExtRepository drugdepositDrugExtRepository) {
        this.userService = userService;
        this.drugdepositDrugExtRepository = drugdepositDrugExtRepository;
    }

    @Override
    public void saveDrugdeposit(DrugdepositEntity entity) {
        // 登记人（当前登录用户）
        UserEntity userEntity = userService.getUserByIdOrAccount(getPrincipal().getName());
        entity.setRegistor(userEntity.getId());
        if (!save(entity)) {
            throw new RuntimeException("保存信息失败");
        }
    }

    @Override
    public void updateDrugdeposit(DrugdepositEntity entity) {
        // 使用 updateById(entity); 修改数据时，将会修改 entity 对象中所有非null数据，如果某个字段为null，将会忽略该字段的修改
        if (!updateById(entity)) {
            throw new RuntimeException("修改信息失败");
        }
    }

    @Override
    public void deleteDrugdeposit(String primaryKey) {
        removeById(primaryKey);
        // 删除包括的缴存药品关联表的信息
        List<DrugdepositDrugExtEntity> depositDds = drugdepositDrugExtRepository.getDepositDds(primaryKey);
        System.out.println("depositDds" + depositDds);
        if (!depositDds.isEmpty()) {
            List<String> depositDdId = depositDds.stream().map(DrugdepositDrugExtEntity::getId).collect(Collectors.toList());
            drugdepositDrugExtRepository.deleteBatchIds(depositDdId);
        }
    }

    @Override
    public void deleteDrugdeposit(List<String> primaryKeys) {
        removeByIds(primaryKeys);
    }

    @Override
    public IPage<DrugdepositVO> listPage(Page<? extends DrugdepositEntity> page, Wrapper<? extends DrugdepositEntity> wrapper) {
        return baseMapper.listPage(page, wrapper);
    }
}